/**
 * 
 */
package com.turtledove.storyalbum.control;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.turtledove.storyalbum.R;
import com.turtledove.storyalbum.common.ConstCommon;
import com.turtledove.storyalbum.model.adapter.AdapterFolder;
import com.turtledove.storyalbum.model.object.ObjectFolder;
import com.turtledove.storyalbum.model.object.ObjectImage;

public class FolderActivity extends Activity{

	GridView gridFolder;
	AdapterFolder adapter;
	ArrayList<ObjectFolder> arrayFolder;
	ArrayList<ObjectImage> arrayImage;
	Context mContext;
	int CREATE_NEW_ALBUM = 1, UPDATE_ALBUM = 2;
	String KEY_FROM_WHERE;
	int KEY_ID_ALBUM;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_folder);
		mContext = this;
		initDB();
		initWidget();
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == CREATE_NEW_ALBUM) {
			if (resultCode == RESULT_OK) {
				String maxIdAlbum = data.getStringExtra(ConstCommon.KEY_RESULT_ID_ALBUM);
				Intent returnIntent = new Intent();
				returnIntent.putExtra(ConstCommon.KEY_RESULT_ID_ALBUM, maxIdAlbum);
				setResult(RESULT_OK, returnIntent);
				finish();
			}
		}
	}
	
	private void initDB()
	{
		Intent intent = new Intent();
		intent = getIntent();
		String keyFromAlbum = intent.getExtras().getString(ConstCommon.KEY_FROM_WHERE_ALBUM);
		String keyFromItem = intent.getExtras().getString(ConstCommon.KEY_FROM_WHERE_ITEM);
		if(keyFromAlbum != null && keyFromAlbum.equals(ConstCommon.KEY_FROM_WHERE_ALBUM))
		{
			KEY_FROM_WHERE = ConstCommon.KEY_FROM_WHERE_ALBUM;
		}else if(keyFromItem != null && keyFromItem.equals(ConstCommon.KEY_FROM_WHERE_ITEM))
		{
			KEY_FROM_WHERE = ConstCommon.KEY_FROM_WHERE_ITEM;
			KEY_ID_ALBUM = intent.getExtras().getInt(ConstCommon.KEY_FROM_WHERE_ITEM_ID_ALBUM);
		}
	}

	private void initWidget() {
		gridFolder = (GridView) findViewById(R.id.listFolder);
		gridFolder.setFastScrollEnabled(true);
		arrayFolder = new ArrayList<ObjectFolder>();
		arrayFolder = getListFolder();
		adapter = new AdapterFolder(this, arrayFolder);
		gridFolder.setAdapter(adapter);
		gridFolder.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				ConstCommon.listImageFromFolder = new ArrayList<ObjectImage>();
				ConstCommon.listImageFromFolder = arrayFolder.get(position).getListImage();
				
				Intent intent = new Intent(FolderActivity.this, AlbumAddNewImageActivity.class);
				intent.putExtra(ConstCommon.KEY_FROM_WHERE_ALBUM, ConstCommon.KEY_FROM_WHERE_ALBUM);
				startActivityForResult(intent, CREATE_NEW_ALBUM);
			}
		});
	}

	ArrayList<ObjectImage> getListImage() {
		ArrayList<ObjectImage> listImage = new ArrayList<ObjectImage>();
		try {
			final String[] columns = { MediaStore.Images.Media.DATA,
					MediaStore.Images.Media._ID };
			final String orderBy = MediaStore.Images.Media._ID;
			@SuppressWarnings("deprecation")
			Cursor imagecursor = managedQuery(
					MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
					null, null, orderBy);
			if (imagecursor != null && imagecursor.getCount() > 0) {

				while (imagecursor.moveToNext()) {
					int dataColumnIndex = imagecursor
							.getColumnIndex(MediaStore.Images.Media.DATA);
					String urlImage = imagecursor.getString(dataColumnIndex);

					String[] strSplit = urlImage.split("/");
					if (strSplit.length > 2) {
						ObjectImage mObjectImage = new ObjectImage();
						mObjectImage.setUrlImage(urlImage);
						mObjectImage.setIsSelected(false);
						String nameFolder = strSplit[(strSplit.length - 2)];
						mObjectImage.setNameFolder(nameFolder);
						listImage.add(mObjectImage);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return listImage;
	}

	boolean checkNameFolderExist(ArrayList<String> listNameFolder,
			String nameFolder) {
		return listNameFolder.contains(nameFolder);
	}

	ArrayList<ObjectFolder> getListFolder() {
		ArrayList<ObjectFolder> listFolder = new ArrayList<ObjectFolder>();
		try {
			ArrayList<ObjectImage> listImg = getListImage();
			ArrayList<String> listNameFolder = new ArrayList<String>();

			// get name folder
			for (int i = 0; i < listImg.size(); i++) {
				String nameFolder = listImg.get(i).getNameFolder();
				if (checkNameFolderExist(listNameFolder, nameFolder) == false) {
					listNameFolder.add(nameFolder);
				}
			}
			for (int i = 0; i < listNameFolder.size(); i++) {
				String nameFolder = listNameFolder.get(i);
				ObjectFolder mObjectFolder = new ObjectFolder();
				mObjectFolder.setNameFolder(nameFolder);
				ArrayList<ObjectImage> listImage = new ArrayList<ObjectImage>();
				for (int j = 0; j < listImg.size(); j++) {
					ObjectImage mObjectImage = new ObjectImage();
					mObjectImage = listImg.get(j);
					if (mObjectImage.getNameFolder().equals(nameFolder)) {
						listImage.add(mObjectImage);
					}
				}
				mObjectFolder.setListImage(listImage);
				listFolder.add(mObjectFolder);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return listFolder;
	}
}
